//
// Copyright (C) 2011 - 2013  Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.2 with QuantLib 1.2.1
//#include "stdafx.h"
#include "JumpDiffusionEngine.h"
using namespace Cephei::QL::Pricingengines::Vanilla;
#include <gen/QL/Processes/Merton76Process.h>
using namespace Cephei::QL::Processes;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (Cephei::QL::Processes::IMerton76Process^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ relativeAccuracy_, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations) 
{
	_pSpinlock = new boost::detail::spinlock ();
    CMerton76Process^ _Cprocess;
    try
    {
#ifdef HANDLE
        _phJumpDiffusionEngine = NULL;
#endif
        _Cprocess = safe_cast<CMerton76Process^> (process);
        _Cprocess->Lock();
        boost::shared_ptr<QuantLib::Merton76Process>& _process = static_cast<boost::shared_ptr<QuantLib::Merton76Process>&> (_Cprocess->GetShared ()); 
        QuantLib::Real _relativeAccuracy_ = 
            (Microsoft::FSharp::Core::FSharpOption<Double>::IsSome::get (relativeAccuracy_) ? (QuantLib::Real)ValueHelper::Convert (relativeAccuracy_->Value) : 1e-4); //4
        QuantLib::Size _maxIterations = 
            (Microsoft::FSharp::Core::FSharpOption<UInt64>::IsSome::get (maxIterations) ? (QuantLib::Size)ValueHelper::Convert (maxIterations->Value) : 100); //4
        _ppJumpDiffusionEngine = new boost::shared_ptr<QuantLib::JumpDiffusionEngine> (new QuantLib::JumpDiffusionEngine ( _process,  _relativeAccuracy_,  _maxIterations ));
        
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cprocess != nullptr) _Cprocess->Unlock();
    }
}
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (boost::shared_ptr<QuantLib::JumpDiffusionEngine>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phJumpDiffusionEngine = NULL;
#endif
	_ppJumpDiffusionEngine = &childNative;
    
}
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (QuantLib::JumpDiffusionEngine& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phJumpDiffusionEngine = NULL;
#endif
	_ppJumpDiffusionEngine = new boost::shared_ptr<QuantLib::JumpDiffusionEngine> (&childNative);
    
    _JumpDiffusionEngineOwner = owner;
    
}

Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (CJumpDiffusionEngine^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phJumpDiffusionEngine = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppJumpDiffusionEngine = new boost::shared_ptr<QuantLib::JumpDiffusionEngine> (copy->GetShared());
        
    }
}
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (PLATFORM::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phJumpDiffusionEngine = NULL;
#endif
	if (!t->IsSubclassOf(CJumpDiffusionEngine::typeid))
		throw REFNEW Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (QuantLib::Handle<QuantLib::JumpDiffusionEngine>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phJumpDiffusionEngine = &childNative;
	_ppJumpDiffusionEngine = &static_cast<boost::shared_ptr<QuantLib::JumpDiffusionEngine>>(childNative.currentLink());
    
    _JumpDiffusionEngineOwner = owner;
}
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (QuantLib::Handle<QuantLib::JumpDiffusionEngine> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phJumpDiffusionEngine = &childNative;
	_ppJumpDiffusionEngine = &static_cast<boost::shared_ptr<QuantLib::JumpDiffusionEngine>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::CJumpDiffusionEngine (QuantLib::JumpDiffusionEngine childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phJumpDiffusionEngine = NULL;
#endif
	_ppJumpDiffusionEngine = new boost::shared_ptr<QuantLib::JumpDiffusionEngine> (new QuantLib::JumpDiffusionEngine (childNative));
    
}
#endif

Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::~CJumpDiffusionEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppJumpDiffusionEngine != NULL)
    {
	    delete _ppJumpDiffusionEngine;
        _ppJumpDiffusionEngine = NULL;
    }
}
Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::!CJumpDiffusionEngine ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppJumpDiffusionEngine != NULL)
    {
	    delete _ppJumpDiffusionEngine;
    }
}
QuantLib::JumpDiffusionEngine& Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::GetReference ()
{
    if (_ppJumpDiffusionEngine == NULL) throw REFNEW NativeNullException ();
	return **_ppJumpDiffusionEngine;
}
boost::shared_ptr<QuantLib::JumpDiffusionEngine>& Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::GetShared ()
{
    if (_ppJumpDiffusionEngine == NULL) throw REFNEW NativeNullException ();
	return *_ppJumpDiffusionEngine;
}
QuantLib::JumpDiffusionEngine* Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::GetPointer ()
{
    if (_ppJumpDiffusionEngine == NULL) throw REFNEW NativeNullException ();
	return &**_ppJumpDiffusionEngine;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::JumpDiffusionEngine>& Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::GetHandle ()
{
	if (_phJumpDiffusionEngine == NULL)
	{
		_phJumpDiffusionEngine = new Handle<QuantLib::JumpDiffusionEngine> (*_ppJumpDiffusionEngine);
	}
	return *_phJumpDiffusionEngine;
}
#endif
bool Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::HasNative () 
{
	return (_ppJumpDiffusionEngine != NULL);
}

Cephei::QL::Pricingengines::Vanilla::IJumpDiffusionEngine^ Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine::Calculate::get ()
{
    try
    {
    	(*_ppJumpDiffusionEngine)->calculate ( );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String(_error.what()));
        else
		    throw REFNEW NativeExcpetion (REFNEW PLATFORM::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

Cephei::QL::Pricingengines::Vanilla::IJumpDiffusionEngine^ Cephei::QL::Pricingengines::Vanilla::CJumpDiffusionEngine_Factory::Create (Cephei::QL::Processes::IMerton76Process^ process, Microsoft::FSharp::Core::FSharpOption<Double>^ relativeAccuracy_, Microsoft::FSharp::Core::FSharpOption<UInt64>^ maxIterations)
{
    return REFNEW CJumpDiffusionEngine ( process,  relativeAccuracy_,  maxIterations);
}
